package com.jtx.miaoah2.Service.Impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jtx.miaoah2.Cache.HotCache;
import com.jtx.miaoah2.Cache.HotKeyLocalCache;
import com.jtx.miaoah2.Common.BaseResponse;
import com.jtx.miaoah2.Mapper.SkGoodsMapper;
import com.jtx.miaoah2.Model.goods.DO.SkGoods;
import com.jtx.miaoah2.Service.SkGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author 季台星
 * @Date 2023 07 23 15 57
 */
@Service
public class SkGoodsServiceImpl extends ServiceImpl<SkGoodsMapper, SkGoods> implements SkGoodsService {
    @Autowired
    private SkGoodsMapper skGoodsMapper;

    @Autowired
    HotKeyLocalCache hotKeyLocalCache;
    @Override
    public BaseResponse getGoodsPage(Integer pageNo, Integer pageSize) {
        //校验参数
        IPage<SkGoods> skGoodsIPage = new Page<>(pageNo,pageSize);
        skGoodsIPage = skGoodsMapper.selectPage(skGoodsIPage,null);
        return BaseResponse.success(skGoodsIPage);
    }

    @Override
    public SkGoods getGoodsById(Long id) {
        String skGoods = hotKeyLocalCache.getGoodsInfoByLocalCache(id);
        if (!Objects.isNull(skGoods)){
            return JSON.parseObject(skGoods, SkGoods.class);
        }
        //到Redis中去查

        return skGoodsMapper.selectById(id);
    }
}
